(function() {
	if (!window.elwiki) window.elwiki = {}; // Создать 'корневой объект' elwiki (:FVK:)

	/*
	 * @class Instances of PageEditor represent the server-side counterpart of a PageEditor widget
	 */
	elwiki.PageEditor = function(properties) {
		bindAll(this, [ "onChange", "onReady", "onRender", "onSend", "layout", "destroy" ]);// @custom
		this.parent = rap.getObject(properties.parent);
		this.element = document.createElement("DIV");
		this.parent.append(this.element);
		this.parent.addListener("Resize", this.layout);
		this.parent.addListener("Dispose", this.destroy);
		rap.on("render", this.onRender); // Render interface
	}

	elwiki.PageEditor.prototype = {

		ready : false,

		onChange : function() {
			console.log("=== elwiki.PageEditor :: onChange()");
		},

		onReady : function() {
			this.ready = true;
			this.layout();
			if (this._text) {
				this.setText(this._text);
				delete this._text;
			}
			/*if (this._font) {
				this.setFont(this._font);
				delete this._font;
			}
			if (typeof this._editable !== "undefined") {
				this.setEditable(this._editable);
				delete this._editable;
			}
			*/
		},

		onRender : function() {
			if (this.element.parentNode) {
				rap.off("render", this.onRender);

				this.editor = ELEDITOR.appendTo(this.element);
				// this.editor.on("instanceReady", this.onReady); // :FVK: from CKeditor in RAP
				this.onReady(); // WORKAROUND.

				rap.on("send", this.onSend);
			}
		},

		onSend : function() {
			console.log("=== elwiki.PageEditor :: onSend()");
			if (this.editor.checkDirty()) { // :FVK: from CKeditor in RAP.
//				var remoteObject = rap.getRemoteObject( elwikiPageEditor );
//				remoteObject.set("text", this.editor.getData());
				this.editor.resetDirty();
			}
		},

		layout : function() {
			if (this.ready) {
				var area = this.parent.getClientArea();
				this.element.style.left = area[0] + "px";
				this.element.style.top = area[1] + "px";
				this.editor.resize(area[2], area[3]);
			}
		},

	    destroy : function() {
	    	console.log("=== elwiki.PageEditor :: destroy()");
	    	/* :FVK: from CKeditor in RAP
	    	if (this.element.parentNode) {
				rap.off("send", this.onSend);
				this.editor.destroy();
				this.element.parentNode.removeChild(this.element);
			}
			*/
		},

		// -- handler's properties ----------------------------------

		setPagelink : function(text) {
			console.log(text);
			this.editor.putPageLink(text);
		},

		setText : function(text) {
			/*if( this.ready ) {
			}*/
			this.editor.setData(text);
		},

		// -- handler's methods -------------------------------------

	};

	elwiki.PageEditor.save = function() {
		var remoteObject = rap.getRemoteObject( elwikiPageEditor );
		remoteObject.notify("save");
	}

	elwiki.PageEditor.cancel = function() {
		var remoteObject = rap.getRemoteObject( elwikiPageEditor );
		remoteObject.notify("cancel");
	}

	// -- helpers ---------------------------------------------------

	var bindAll = function(context, methodNames) {
		for (var i = 0; i < methodNames.length; i++) {
			var method = context[methodNames[i]];
			context[methodNames[i]] = bind(context, method);
		}
	};

	var bind = function(context, method) {
		return function() {
			return method.apply(context, arguments);
		};
	};

	var async = function(context, func) {
		window.setTimeout(function() {
			func.apply(context);
		}, 0);
	};

}());
